package com.ermax.aircond.maintain.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ermax.aircond.maintain.dao.FloorDao;
import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.maintain.domain.Floor;
import com.ermax.aircond.util.dao.AbstractHibernateDao;

@Repository("floorDao")
public class FloorDao extends AbstractHibernateDao<Floor> implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	
	public FloorDao(){
		setClazz(Floor.class);
	}
	
	public List<Floor> findAll(String status){
		Criteria criteria = this.getCurrentSession().createCriteria(Floor.class);
		if(isNotNull(status)){
			criteria.add(Restrictions.eq("status", status));
		}
		criteria.addOrder(Order.asc("building"));
		criteria.addOrder(Order.asc("floorNo"));
		
		@SuppressWarnings("unchecked")
		List<Floor> results = criteria.list();		
		return results;
	}
	
	public List<Floor> findFloors(Building building, String keyword, String status) {
		Criteria criteria = this.getCurrentSession().createCriteria(Floor.class);
		
		if(isNotNull(building)){
			criteria.add(Restrictions.eq("building", building));
		}
		
		if(isNotNull(keyword)){
			StringBuilder sbKey = new StringBuilder();
			sbKey.append("%");
			sbKey.append(keyword);
			sbKey.append("%");
			String key = sbKey.toString();
						
			criteria.add(Restrictions.like("floorName", key).ignoreCase());
			criteria.add(Restrictions.like("description", key).ignoreCase());			
		}
		
		if(isNotNull(status)){
			criteria.add(Restrictions.eq("status", status));
		}
		
		criteria.addOrder(Order.asc("building"));
		criteria.addOrder(Order.asc("floorNo"));
		
		@SuppressWarnings("unchecked")
		List<Floor> results = criteria.list();		
		return results;
	}
		

}
